﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ParameterDisplayUtils.h"
#include "GcmpModel.h"
#include "IParameterDefinition.h"
#include "IParameter.h"
#include "ParameterDisplayCheckbox.h"
#include "ParameterDisplayDropdownList.h"
#include "ParameterDisplayEditbox.h"
#include "ParameterDisplayEditboxWithButton.h"
#include "IParameterValueBool.h"
#include "IParameterDisplayManager.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

namespace
{
    const std::wstring & ToDisplayString(const IDocument* pDoc, const IParameterDefinition* pPD,
        const IParameterValueStorage* pPVS)
    {
        static std::wstring s_tmpReturnString;
        s_tmpReturnString = L"";

        DBG_WARN_AND_RETURN_UNLESS(pDoc, s_tmpReturnString, L"无效的参数pDoc",L"GDMPLab",L"2024-03-30");
        DBG_WARN_AND_RETURN_UNLESS(pPD, s_tmpReturnString, L"无效的参数pPD",L"GDMPLab",L"2024-03-30");

        IParameterDisplayManager* pParamDisplayMgr = IParameterDisplayManager::Get(pDoc);
        DBG_WARN_AND_RETURN_UNLESS(pParamDisplayMgr, s_tmpReturnString, L"无效的参数pParamDisplayMgr",L"GDMPLab",L"2024-03-30");

        const IParameterDisplay* pDisplay = pParamDisplayMgr->GetParameterDisplay(pPD);
        if (const ParameterDisplayEditbox* pEditbox =
            dynamic_cast<const ParameterDisplayEditbox*>(pDisplay))
        {
            s_tmpReturnString = pEditbox->GetDisplayStringByValue(pDoc, pPVS);
            return s_tmpReturnString;
        }
        if (const ParameterDisplayEditboxWithButton* pEditboxWithButton =
            dynamic_cast<const ParameterDisplayEditboxWithButton*>(pDisplay))
        {
            s_tmpReturnString = pEditboxWithButton->GetDisplayStringByValue(pDoc, pPVS);
            return s_tmpReturnString;
        }
        else if (const ParameterDisplayDropdownList* pDropdownList =
            dynamic_cast<const ParameterDisplayDropdownList*>(pDisplay))
        {
            s_tmpReturnString = pDropdownList->GetDisplayStringByValue(pDoc, pPVS);
            return s_tmpReturnString;
        }
        else if (const ParameterDisplayCheckbox* pCheckBox =
            dynamic_cast<const ParameterDisplayCheckbox*>(pDisplay))
        {
            const IParameterValueBool* pBool =
                quick_cast<const IParameterValueBool>(pPVS);
            DBG_WARN_AND_RETURN_UNLESS(pBool, s_tmpReturnString, L"pBool为空",L"GDMPLab",L"2024-03-30");
            s_tmpReturnString = pBool->GetValue() ? L"true" : L"false";
            return s_tmpReturnString;
        }

        return s_tmpReturnString;
    }
}

const std::wstring & gcmp::ParameterDisplayUtils::GetValueAsDisplayString(const IParameter* pParam)
{
    static std::wstring s_emptyString;
    DBG_WARN_AND_RETURN_UNLESS(pParam, s_emptyString, L"Input parameter is null.",L"GDMPLab",L"2024-03-30");

    const IParameterDefinition* pd = pParam->GetParameterDefinition();
    DBG_WARN_AND_RETURN_UNLESS(pd && pd->IsValidParameterDefinition(pParam->GetDocument()),s_emptyString,L"未找到ParameterDefinitionObject",L"GDMPLab",L"2024-03-30");

    return ToDisplayString(pParam->GetDocument(), pd, pParam->GetParameterValueStorage());
}

bool gcmp::ParameterDisplayUtils::SetValueFromDisplayString(IParameter* pParam,
    const std::wstring & displayValue)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pParam, L"Input parameter is null.",L"GDMPLab",L"2024-03-30");

    const IParameterDefinition* pd = pParam->GetParameterDefinition();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pd && pd->IsValidParameterDefinition(pParam->GetDocument()),L"未找到ParameterDefinitionObject",L"GDMPLab",L"2024-03-30");


    IParameterDisplayManager* pParamDisplayMgr = IParameterDisplayManager::Get(pParam->GetDocument());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pParamDisplayMgr, L"无效的参数pParamDisplayMgr",L"GDMPLab",L"2024-03-30");
    const IParameterDisplay* pDisplay = pParamDisplayMgr->GetParameterDisplay(pd);
    if (const ParameterDisplayEditbox* pEditboxWithButton =
        dynamic_cast<const ParameterDisplayEditbox*>(pDisplay))
    {
        OwnerPtr<IParameterValueStorage> oPVS =
            pEditboxWithButton->GetValueByDisplayString(pParam->GetDocument(), displayValue);
        if (oPVS)
        {
            pParam->SetParameterValueStorage(TransferOwnership(oPVS));
            return true;
        }
    }
    else if (const ParameterDisplayEditboxWithButton* pEditboxWithButton =
        dynamic_cast<const ParameterDisplayEditboxWithButton*>(pDisplay))
    {
        OwnerPtr<IParameterValueStorage> oPVS =
            pEditboxWithButton->GetValueByDisplayString(pParam->GetDocument(), displayValue);
        if (oPVS)
        {
            pParam->SetParameterValueStorage(TransferOwnership(oPVS));
            return true;
        }
    }

    return false;
}

